package cn.brilliantfuture.commentry.operate.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.ejb.Stateless;
import javax.jws.WebService;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.operate.ControlMessageRemote;
import cn.brilliantfuture.commentry.operate.ControlMessageWebService;
import cn.brilliantfuture.commentry.util.BaseMessageImpl;
import cn.brilliantfuture.commentry.util.HttpUtil;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2011-10-25
 * 
 * Description: 配送柜控制管理
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.operate.ControlMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/ControlMessageWebService")
public class ControlMessageImpl extends BaseMessageImpl implements ControlMessageRemote, ControlMessageWebService{

	public String setAdvertContent(String deviceSn, Integer screenSn,
			Integer inStyle, Integer inSpeed, Integer outStyle,
			Integer outSpeed, Integer stayTime, String content) {
		logger.info("ControlMessageImpl.setAdvertContent() ... ");
		logger.info("deviceSn -> " + deviceSn + " screenSn -> " + screenSn + " inStyle -> " + inStyle + " inSpeed -> " + inSpeed + 
				" outStyle -> " + outStyle + " outSpeed -> " + outSpeed + " stayTime -> " + stayTime + " content -> " + content);
		
		try {
			content = URLEncoder.encode(content, "utf8");
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append(getRemoteUrl());
		sb.append("/set/?cabinetID=");
		sb.append(deviceSn);
		sb.append("&setKey=");
		if (0 == screenSn) {
			sb.append("SETAD");
		} else if (1 == screenSn) {
			sb.append("SETADL");
		} else if (2 == screenSn) {
			sb.append("SETADR");
		}

		sb.append("&inStyle=").append(inStyle);
		sb.append("&inSpeed=").append(inSpeed);
		sb.append("&outStyle=").append(outStyle);
		sb.append("&outSpeed=").append(outSpeed);
		sb.append("&stayTime=").append(stayTime);
		sb.append("&setValue=").append(content);
		logger.info(sb.toString());
		return HttpUtil.getInvoke(sb.toString());
	}

	public String remoteUnlock(String deviceSn, String gridSn) {
		logger.info("ControlMessageImpl.remoteUnlock(String deviceSn, String gridSn) ... ");
		logger.info("deviceSn -> " + deviceSn + " gridSn -> " + gridSn);
		StringBuffer sb = new StringBuffer();
		sb.append(getRemoteUrl());
		sb.append("/open/?cabinetID=");
		sb.append(deviceSn);
		sb.append("&gridId=");
		sb.append(gridSn);
		logger.info(sb.toString());
		return HttpUtil.getInvoke(sb.toString());
	}

	public String remoteControl(String deviceSn, String setKey, String value) {
		logger.info("ControlMessageImpl.remoteControl(String deviceSn, String setKey, String value) ... ");
		logger.info("deviceSn -> " + deviceSn + " setKey -> " + setKey + " value -> " + value);
		StringBuffer sb = new StringBuffer();
		sb.append(getRemoteUrl());
		sb.append("/set/?cabinetID=");
		sb.append(deviceSn);
		sb.append("&setKey=");
		sb.append(setKey);
		sb.append("&setValue=");
		sb.append(value);
		logger.info(sb.toString());
		return HttpUtil.getInvoke(sb.toString());
	}


}
